@extends('home.layout.layout')


@section('con')

<div class="breadcrumbs">
		<div class="container">
			<div class="breadcrumbs-main">
				<ol class="breadcrumb">
					<li><a href="{{url('/')}}">首页</a></li>
					<li class="active">购物车</li>
				</ol>
			</div>
		</div>
	</div>
	<br>

		
	<div class=" cart-items heading">
			 <h3 style="color:#8C2830">&nbsp;&nbsp;&nbsp;我的购物车</h3>
	<div>
		<img src="{{asset('/HomeCss/images/1.gif')}}" alt="" style="position:absolute; left:500px ">
	</div>
	<br>
	<br>
				<script>$(document).ready(function(c) {
					$('.close1').on('click', function(c){
						$('.cart-header').fadeOut('slow', function(c){
							$('.cart-header').remove();
						});
						});	  
					});
			   </script>
			<script>$(document).ready(function(c) {
					$('.close2').on('click', function(c){
						$('.cart-header1').fadeOut('slow', function(c){
							$('.cart-header1').remove();
						});
						});	  
					});
			   </script>
			   <script>$(document).ready(function(c) {
					$('.close3').on('click', function(c){
						$('.cart-header2').fadeOut('slow', function(c){
							$('.cart-header2').remove();
						});
						});	  
					});
			   </script>
				
			<form action="{{url('/home/order/add')}}" method="post" >
				<table class="table table-hover zong">
					<tr>
						<td>勾选商品</td>
						<td>图片</td>
						<td>商品名称</td>		
						<td>单价</td>
						<td>数量</td>
						<td>小计</td>
						<td>操作</td>


					</tr>
					@foreach($carts as $k=>$v)
					<tr class = 'trzong'>
							<td id="shangpin" gid="{{$v->gid}}" buycnt="{{$v->buycnt}}">
								<input name="data[{{$v->gid}}][gid]"  value="{{$v->gid}}"  class="goods"  type="checkbox" checked="checked">
							</td>
							<input id = 'eerr' type="hidden" name="cnt" value="{{$v->cnt}}">
							<td>
								<img src="{{$v->gpic}}" width="70px"></a>
							</td>
							<td>{{$v->gname}}</td>
							<td id="danjia">{{$v->price}}</td>
							<td >
								<p class="p_buycnt">
						            <button  type="button" class="sy_minus jian" >-</button> 
						            <input style="width:30px" class="sy_buycnt" id="sy_buycnt_gid" type="text" name="data[{{$v->gid}}][buycnt]" value="{{$v->buycnt}}" /> 
						            <button type = 'button' class="sy_plus jia" >+</button>
						        </p>

							</td>
							<td id="totalPrice" class="xiaoJ" value="{{$v->buycnt * $v->price}}">￥:<b>{{$v->buycnt * $v->price}}</b></td>
							<td>
								<button type="button" class="btn_delete"  gid="{{$v->gid}}">删除</button>
								<button type="button" class="shoucang">收藏</button>
							</td>
					</tr>
					@endforeach
					
					<tr>
						<td style="width:150px; ">
								
						<a href="{{url('/')}}">继续去购物</a>
				
						</td>						
						<td></td>
						<td></td>
						<td id="order"></td>
						<td></td>
						<td id="zongji" style="font-size:20px; color:red;width:265px; font-weight:bold" value="">合计(含运费)
						<span>￥:<?php
							// dd($carts);
							$count='';
							foreach($carts as $k=>$v)
							{
								$count+=$v->buycnt* $v->price;
							}
							echo $count;
						 ?></span>
						元 </td>
						<td style="width:150px"><button class="btn btn-danger btn-lg"  style="font-weight:bold">去结算</button></td>

					</tr>
					{{csrf_field()}}
				</table>
			</form>
				
			
			</div>
			</div>
@endsection

@section('js')
<script type="text/javascript">
	$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
      });

	// ====================================================================================
	//var bbll = 0;
	//判断选框
	$('.goods').click(function()
	{
		// var shuxing = $(this).checked;
		// alert(shuxing);
		// alert(this.checked);
		// alert(typeof(this.checked));
		//获取商品数量和id
		//判断有选框为空时可以结算
		if(this.checked == false)
		{
			//alert(2);
			var gid = $(this).parents('td').removeAttr('gid');
			var buycnt = $(this).parents('tr').find('.sy_buycnt').attr('name','');
			//console.log(buycnt);
			//alert(typeof(buycnt));

		}else{
			
		    //alert(1);
			var gid = $(this).parents('td').attr('gid');
			var buycnt = $(this).parents('td').attr('buycnt');
			
		}		

		// if(this.checked){
		// 	var inp1 = $('<input type="hidden" name="gid[]" class="gid" value="'+gid+'"/>');
		// 	var inp2 = $('<input type="hidden" name="buycnt[]" class="buycnt" value="'+buycnt+'"/>');
		// 	$('#order').append(inp1);
		// 	$('#order').append(inp2);
		// }else{
		// 	$('input[class=gid][value='+gid+']').remove();
		// 	$('input[class=buycnt][value='+buycnt+']').remove();
		// }

         	// //发送ajax请求
         	// $.post('/home/order/add',{gid:gid,buycnt:buycnt},function(data){});
	})
         	

     //判断选框都为空时不能跳往结算页
     var ch = 0;
     $('form').submit(function(){
     	$(this).find('input[type=checkbox]').each(function(){
     		if($(this).attr('checked') == 'checked')
     		return ch = 1;
     	})
     	if(ch){
     		return true; 
     	}else{
     		alert('您还未勾选要购买的商品');
     		return false;
     	}

     })

        
	//==============================================================================
        //点击减号
        $('.jian').click(function(){

            var btn = $(this);
            //点击减号去除加号disabled属性
            btn.parent('p').find('.jia').removeAttr('disabled');
            btn.next().val(parseInt(btn.next().val()) - 1);
            var b = btn.next().val();
            //alert(b);

            if(b<1){
            	btn.next().val(0);

            	$(this).attr('disabled','disabled');
            }else{
            	$(this).removeAttr('style');

            }

            var xiaoji = btn.parents('td').next().html();
            var danjia = btn.parents('td').prev().html();
            var zongji = btn.parents('.zong').find('span').html();
            xiaoji = danjia*b;
            btn.parents('td').next().find('b').html(xiaoji);
            // angji);
            total = 0;
            for (var i = 0; i < $('.trzong').find('.xiaoJ').length; i++) {
            	
            	var num = $('.trzong').find('.xiaoJ').eq(i).find('b').html();
           
            	total = parseFloat(total) + parseFloat(num);
            };

            btn.parents('.zong').find('span').html(total);

            //获取input中的商品id值
            //var gid= $(this).attr('gid');
            // alert(gid);
              // $.get('/home/cart/edit',{gid:gid,a:-1},function(data){
              //   location.reload() 
            //});
        	return false;
        })


        //点击加号
         $('.jia').click(function(){
            var btn = $(this);
            //点击加号去除减号的disabled
            btn.parent('p').find('.jian').removeAttr('disabled');
            //获取input中的商品id值
            // var gid= $(this).attr('gid');
            btn.prev().val(parseInt(btn.prev().val()) + 1);
            var a = btn.prev().val();
            //获取商品库存做判断
            var cnt = btn.parents('tr').find('input[name=cnt]').val();
           	// alert(cnt);
            if(a == cnt){
            	btn.attr('disabled','disabled');
            }
            // alert(a);
			var xiaoji = btn.parents('td').next().html();
            var danjia = btn.parents('td').prev().html();
            var zongji = btn.parents('.zong').find('span').html();
			// alert(xiaoji);
             xiaoji = danjia * a;

             //alert(danjia);
             btn.parents('td').next().find('b').html(xiaoji);
             
             
             var total = 0;
            
         	
             //商品数
         	// alert($('.trzong').find('#totalPrice').length);
         	// console.log(total);
         	// alert(222);

         	// var  cc = btn.parents('td').next().html();
         		// alert(cc);
         		//console.log(total);
            	for(var i = 0; i < $('.trzong').find('.xiaoJ').length; i++) 
            	{
            		var num = $('.trzong').find('.xiaoJ').eq(i).find('b').html();
            		total = parseFloat(total) + parseFloat(num);
            		 // console.log(total);
            		// total += $('.trzong').find('#totalPrice').html();
            	}
            		//console.log(total);
            		 // alert(total);
          
             	btn.parents('.zong').find('span').html(total);

            // zongji = zongji + xiaoji;
            //  $('#totalPrice').html(xiaoji);
            //  $('#zongji').html(zongji);
             //alert(xiaoji);
            //   $.get('/home/cart/edit',{gid:gid,a:+1},function(data){
            //     location.reload() 
            // });


         	return false;
        	

        })
       

         // ======================================================================================
		
         //删除
         $('.btn_delete').click(function(){
         	var gid = $(this).attr('gid');
         	 //console.log(gid);
         	var btn = $(this);
         	//发送ajax请求
         	$.post('/home/cart/delete',{gid:gid},function(data){
         		if(data == 1){
         			alert('删除成功');
         			btn.parents('tr').remove();
         		}else{
         			alert('删除失败');
         		}

         	})

         })
         //============================================================================================
        


        // $('.gg').click(function(){
        // 	if($('.gg').attr('checked')){
        // 		$('.gg').attr('checked' = false);
        // 	}else{
        // 		$('.gg').attr('checked'= true);
        // 	}
       
        // })



		//收藏
		$('.shoucang').click(function(){
			var gid = $(this).parents('tr').find('input').val();
			
			$.get('/home/goods/collectadd',{gid:gid},function(data){
				if(data ==1 ){
					alert('收藏成功');
				}else{
					alert('商品已收藏,再看看别的商品吧');

				}

			});


		})
	



</script>
@endsection
      